package com.alan.crash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class CrashHandler implements UncaughtExceptionHandler {

    private final String TAG = getClass().getSimpleName();

    //系统默认的UncaughtException处理类
    private UncaughtExceptionHandler mDefaultHandler;
    //CrashHandler实例
    private static CrashHandler INSTANCE;
    //程序的Context对象
    private Context mContext;
    //用来存储设备信息和异常信息
    private Map<String, String> exceptInfo = new HashMap<String, String>();

    private OnCrashListener onCrashListener;

    private boolean isSaveFile = true;

    private CrashHandler() {
    }

    /**
     * 获取CrashHandler实例 ,单例模式
     */
    public static CrashHandler getInstance() {
        if (INSTANCE == null) INSTANCE = new CrashHandler();
        return INSTANCE;
    }

    /**
     * @author YangZhenYu
     * created at 2017/3/22 21:50
     * 功能：
     */
    public void init(Context context) {
        mContext = context;
        //获取系统默认的UncaughtException处理器
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        //设置自己的这个CrashHandler为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    /**
     * 当有未捕获的异常发生时执行该函数来处理
     */
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // 打印异常日志
        if (null != ex) {
            Log.e(TAG, Log.getStackTraceString(ex));
        }

        //自定义的CrashHandler来处理未捕获的异常，如果异常处理的将返回true
        String s = handleException(ex);
        if (null != onCrashListener) {
            onCrashListener.onCrashListener(thread, s, ex);
        }
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
     *
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false.
     */
    private String handleException(Throwable ex) {
        if (ex == null) {
            return "";
        }
        //收集设备参数信息
        collectDeviceInfo(mContext);
        //保存日志文件
        return saveCrashInfo2File(ex);

    }

    /**
     * 收集设备参数信息
     *
     * @param ctx
     */
    public void collectDeviceInfo(Context ctx) {
        try {
            PackageManager pm = ctx.getPackageManager();
            PackageInfo info = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);
            if (info != null) {
                String versionName = info.versionName == null ? "null" : info.versionName;
                String versionCode = info.versionCode + "";
                exceptInfo.put("versionName", versionName);
                exceptInfo.put("versionCode", versionCode);
            }
        } catch (NameNotFoundException e) {
            Log.i(TAG, Log.getStackTraceString(e));
        }
        //把整个Build类厘米那的信息都拿出来，如手机品牌、型号、系统版本等等信息
        Field[] fields = Build.class.getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                exceptInfo.put(field.getName(), field.get(null).toString());
            } catch (Exception e) {
                Log.i(TAG, Log.getStackTraceString(e));
            }
        }
    }

    /**
     * 保存错误信息到文件中
     *
     * @param ex
     * @return 返回文件名称, 便于将文件传送到服务器
     */
    private String saveCrashInfo2File(Throwable ex) {

        StringBuffer sb = new StringBuffer();
        sb.append("--------------start---------------");
        for (Map.Entry<String, String> entry : exceptInfo.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            sb.append(key + "=" + value + "\n");
        }

        Writer writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        //将异常信息写入printWriter中
        ex.printStackTrace(printWriter);

        Throwable cause = ex.getCause();
        while (cause != null) {
            //把异常原因写入printWriter中，其实写上面的异常信息就足够了
            cause.printStackTrace(printWriter);
            cause = cause.getCause();
        }
        printWriter.close();
        String result = writer.toString();
        sb.append(result);
        sb.append("-------------end---------------");
        Log.e(TAG, "---" + sb.toString());//此处就可以将异常信息发送给服务器

        if (isSaveFile) {
            saveToFile(sb.toString());
        }
        return sb.toString();
    }

    public interface OnCrashListener {

        void onCrashListener(Thread thread, String err, Throwable throwable);
    }

    /**
     * 异常处理方式 1 保存到文件
     *
     * @param text 异常信息
     */
    public void saveToFile(String text) {
        String path = mContext.getCacheDir().getPath() + "/error" + System.currentTimeMillis() + ".txt";
        File file = new File(path);
        try {
            if (!file.exists()) {
                boolean newFile = file.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(file, true);
            fileWriter.write(text);
            fileWriter.close();
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
    }

    /**
     * 异常处理方式 2 退出系统
     */
    public void exitSystem() {
        //退出程序
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(1);
    }

    /**
     * 异常处理方式 3 系统默认的异常处理器来处理
     *
     * @param thread 异常抛出的线程
     * @param ex     异常信息类
     */
    public void defaultHandler(Thread thread, Throwable ex) {
        //没有处理则用系统默认的异常处理器来处理
        if (mDefaultHandler != null) {
            mDefaultHandler.uncaughtException(thread, ex);
        }
    }

    public void setOnCrashListener(OnCrashListener onCrashListener) {
        this.onCrashListener = onCrashListener;
        this.isSaveFile = false;
    }

    public void setSaveFile(boolean saveFile) {
        isSaveFile = saveFile;
    }
}